<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/css/table.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/dwz/themes/default/style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="${pageContext.request.contextPath}/dwz/themes/css/core.css" rel="stylesheet" type="text/css" media="screen" />
<script src="${pageContext.request.contextPath}/jquery/jquery-2.1.4.min.js" type="text/javascript"></script>

<form id="permissionData">
<table border="0" cellpadding="0" cellspacing="0" class="table_border">
	<tr>
		<td>
			<table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_right">
				<tr>
					<td><font style="font-size: 12px;"><strong>新增许可</strong></font>
					</td>
					<td width="25" height="26" align="left"></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<table width="100%" border="0" cellspacing="0" class="table_right">
				<tr>
					<td align="center">



						<table border="0" cellpadding="0" cellspacing="0"
							class="box_table" id="box_table2">
							<thead>
								<tr>
									<td height="36" class="box_table_even">代码：</td>
									<td class="box_table_odd">
										<input id="code" name="code" onblur="checkCode()" type="text" />
										<span class="star">*</span>
										<span id="codeMesg"></span>
									</td>
								</tr>
								<tr>
									<td height="36" class="box_table_even">名称：</td>
									<td class="box_table_odd">
										<input id="name" name="name" onblur="checkName()" type="text" />
										<span class="star">*</span>
										<span id="nameMesg"></span>
									</td>
								</tr>
								<tr>
									<td class="box_table_even">模块URL：</td>
									<td class="box_table_odd">
										<input id="moduleUrl" name="moduleUrl"  type="text" />
									</td>
								</tr>
								<tr>
									<td class="box_table_even">操作URL：</td>
									<td class="box_table_odd">
										<input id="operationUrl" name="operationUrl"  type="text" size="90" />
										多个逗号隔开
									</td>
								</tr>
								<tr>
						  			<td class="box_table_even">排序号：</td>
									<td class="box_table_odd">
										<input id="orderNo" name="orderNo" onblur="checkOrderNo()" type="text"/>
										<span id="orderNoMesg"></span>
									</td>
								</tr>
							</thead>
							<tbody>
							</tbody>
						</table>

						<table border="0" cellpadding="0" cellspacing="0"
							class="operation">
							<thead>
								<tr>
									<td height="24">&nbsp;</td>
								</tr>
							</thead>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<table width="100%" border="0" cellpadding="0" cellspacing="0"
				class="table_padding">
				<tr>
					<td height="21" align="right">
						<table align="left">
							<tr>
								<td width="50px"><a class="button" onclick="save()" href="javascript:void(0)"><span>保存</span></a></td>
								<td width="50px"><a class="button" href="javascript:void(window.history.back());"><span>返回</span></a></td>
								<span id="mesg" style="color: red"></span>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</form>
<script>
	function checkForm(){
		return checkCode() && checkName() && checkOrderNo();
	}
	function checkOrderNo() {
		var regExp = /^\d{1,3}$/;
		var orderNoVal = $("#orderNo").val();
		var flag = regExp.test(orderNoVal);
		showMesg("orderNoMesg",flag,"需要1-3位的数字");
		return flag;
	}

	function checkName() {
		var regExp = /^[a-zA-Z]{3,5}$/;
		var nameVal = $("#name").val();
		var flag = regExp.test(nameVal);
		if (flag){
			<%--$.get("permission?method=getPermissionNameAndPid&pId=${pId}&name="+nameVal,function (result) {--%>
			<%--	if (result.code=="500"){--%>
			<%--		flag = false;--%>
			<%--	}--%>
			<%--	showMesg("nameMesg",flag,"权限名已存在");--%>
			<%--	return flag;--%>
			<%--});--%>
			$.ajax({
				async: false,
				type: "GET",
				url: "permission?method=getPermissionNameAndPid&pId=${pId}&name="+nameVal,
				success:function (result){
					if (result.code=="500"){
						flag = false;
					}
					showMesg("nameMesg",flag,"权限名已存在");
				},

			}).responseText;
		}else {
			showMesg("nameMesg",flag,"名称为3-5位的字母");
		}
		return flag;
	}

	function checkCode() {
		var regExp = /^\d{1,10}$/;
		var codeVal = $("#code").val();
		var flag = regExp.test(codeVal);
		showMesg("codeMesg",flag,"代码需要1-10位的数字");
		return flag;
	}

	function showMesg(id,flag,info){
		var showEle = document.getElementById(id);
		showEle.innerText = flag?"数据合法":info;
		showEle.style.color = flag?"green":"red";
	}

	function save(){
		$.ajax({
			type:"POST",
			url:"permission",
			data:{
				method:"save",
				pId:"${pId}",
				code:$("#code").val(),
				name:$("#name").val(),
				moduleUrl:$("#moduleUrl").val(),
				operationUrl:$("#operationUrl").val(),
				orderNo:$("#orderNo").val()
			},
			beforeSend:function(){
				var flag = checkForm();

				$("#mesg").text(flag?"正在保存信息，请稍后...":"请填入合法数据");
				return flag;
			},
			success:function (data){
				if(data.code==200){
					$("#mesg").text("保存成功")
					var treeObj = parent.treeFrame.$.fn.zTree.getZTreeObj("permission_tree");
					var parentNode = treeObj.getSelectedNodes()[0];
					var childNode = {
						name: $("#name").val(),
						id:data.id
					}
					treeObj.addNodes(parentNode, childNode);

				}else {
					$("#mesg").text("保存失败")
				}
			}
		});
	}
</script>